package eg.edu.eelu.fyp2013.jdetector.core.swt;

import java.util.ArrayList;

import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

import PCANeural.PCANeuralMCRFactory;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
import matlabcontrol.extensions.MatlabNumericArray;
import matlabcontrol.extensions.MatlabTypeConverter;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.ClassiferData;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.FeatureExtraction;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.ReduceNeuralData;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.ReducedData;
import PCANeural.PCARNeural;

public class ReducePCANeural {
	
	public ClassiferData [] reducefeatures (ArrayList<ArrayList<FeatureExtraction>> FeaturedMatixList
			,int levels ) throws MWException {
	{
	
		
       
		ReduceNeuralData [][] Alll = null;
		 
		 int sizeimages = 0;
		 int sizelevels = 0 ;
		for(int i = 0 ; i < FeaturedMatixList.size() ; i++) // num of levels
		{
		
			if( i == 0)
			{
			 Alll = new ReduceNeuralData [FeaturedMatixList.get(i).size()][FeaturedMatixList.size()];
			 sizeimages = FeaturedMatixList.get(i).size();
			 sizelevels = FeaturedMatixList.size();
			}
			
				for(int j = 0; j < FeaturedMatixList.get(i).size(); j++) // num of images 
				   {
			        
				 //   processor.setNumericArray("Horz", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImagechd,null));
				//    processor.setNumericArray("vert", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImagecvd,null));
				//    processor.setNumericArray("diag", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImagecdd,null));
				//    processor.setNumericArray("All", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImageAll,null));
				//    proxy.eval("[H , V , D ,All] = PCAReduceNeural(Horz, vert, diag, All)");
				    
					PCARNeural pNe = new PCARNeural();
					Object [] PC = new Object [4];
				    PC[0] = FeaturedMatixList.get(i).get(j).extractImagechd;
				    PC[1] = FeaturedMatixList.get(i).get(j).extractImagecvd;
				    PC[2] = FeaturedMatixList.get(i).get(j).extractImagecdd;
				    PC[3] = FeaturedMatixList.get(i).get(j).extractImageAll;
				    
				    MWNumericArray [] M = new MWNumericArray[4];
				    
				    
				    
				    pNe.PCAReduceNeural(M,PC);
				    
				    double [] H = M[0].getDoubleData();
				    double [] V = M[1].getDoubleData();
				    double [] D = M[2].getDoubleData();
				    double [] all = M[3].getDoubleData();
				    
				    ReduceNeuralData R = new ReduceNeuralData();
				    
				    R.horz = new double[256];
				    R.vert = new double[256];
				    R.diag = new double[256];
				    R.all = new double [256];
				    
				    
				    R.horz = H;
				    R.vert= V;
				    R.diag = D;
				    R.all =  all;
				    
				    R.label = FeaturedMatixList.get(i).get(j).label;
				    R.level = i;
	                
				
				    
				   // Alll[j][i].horz = new double [256][1];
				  //  Alll[j][i].vert = new double [256][1];
				  //  Alll[j][i].diag = new double [256][1];
				  //  Alll[j][i].all = new double [256][1];
				    Alll[j][i] = R;
				    
				  /*  for(int kk = 0 ; kk < 256; kk++)
				    {
				    	Alll[j][i].horz[kk][0] = R.horz[kk][0];
					    Alll[j][i].vert[kk][0] = R.vert[kk][0];
					    Alll[j][i].diag[kk][0] = R.diag[kk][0];
					    Alll[j][i].all[kk][0] = R.all[kk][0];
				    	
				    }*/
				    
				    
				   
				    
			  
			}
			
			
			
		}
		
		
		ClassiferData [] FinalFeature = new ClassiferData[sizeimages];
        for(int m = 0; m < sizeimages; m++)
	    {
        	ClassiferData CD = new ClassiferData();
        	CD.Allreduceddata = new ArrayList<Double>();
        	
			for(int n = 0; n < sizelevels; n++)
			{
				
				
				for(int l = 0; l < 255; l++)
				{
				    CD.Allreduceddata.add(Alll[m][n].horz[l]);
				    CD.Allreduceddata.add(Alll[m][n].vert[l]);
				    CD.Allreduceddata.add(Alll[m][n].diag[l]);
				    CD.Allreduceddata.add(Alll[m][n].all[l]);
				}
				
			
			}
			CD.label = Alll[m][sizelevels - 1].label;
			FinalFeature[m] = CD;
			
	    }
        
        
        
        
		
        System.out.print("PCA two completed");
        return FinalFeature;
		
		
		
	}
	}	

}
